
//  User.m
//  CoolTennisBall
//
//  Created by 谢伟康 on 15/12/23.
//  Copyright © 2015年 CoolLang. All rights reserved.
//

#import "User.h"

@implementation User

+ (NSDictionary *)defaultPropertyValues
{
    return @{
             @"userInforItem":@{
                     @"name": @"",
                     @"location": @"",
                     @"genderType": @0,
                     @"height": @"170",
                     @"weight": @"60",
                     @"birth": @"1995-01-01",
                     @"signature": NSLocalizedString(@"The person is so busy, leaving nothing.", nil),
                     @"headIconPath": @"",
                     @"phoneNum": @"",
                     @"userID": @"",
                     @"creatTime": [NSDate date],
                     @"lasLoginTime": @"",
                     @"lastLoginVersion": @"",
                     @"hand":@1,
                     @"raketBrand":@"",
                     @"ballAge":@"一年以下",
                     @"startTennisYear":@"",
                     @"tennisLevel":@""
                     },
             @"sportRecordItems":@[],
             @"userDevice":@{@"deviceUUID":@"",
                             @"oemType":@2,
                             @"locationVersion":@"",
                             @"serverVersion":@""},
             @"lastUploadTime":@"0",
             @"userEquipModel":@{@"UserID": @"",
                                 @"Brand": @"",
                                 @"VerticalLine": @"",
                                 @"BuyTime":@"",
                                 @"Line": @"",
                                 @"ShoesName": @"",
                                 @"ShoesBuyTime": @"",
                                 @"ShoesUrl": @"",
                                 @"UpdateTime": @"",
                                 @"RacketUrl": @""
                                 }
             
             };
}

+ (NSString *)primaryKey {
    return @"identification";
}

#pragma mark - Public Method

+ (User *)getLastUser
{
    NSString *identification = [DataModel lastLoginID];
    User *user = [User objectForPrimaryKey:identification];
    if (user == nil) {
        user = [[User alloc] init];
        user.identification = [DataModel lastLoginID];
        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm beginWriteTransaction];
        [realm addObject:user];
        [realm commitWriteTransaction];
    }
    
    return user;
}

- (void)addSportRecordItemsObject:(NSArray *)sportRecordItems hasUpLoad:(BOOL)hasUpload
{
    for (SportRecordItem *item in sportRecordItems) {
        if ([item amountOfCount] == 0) {
            continue;
        }
        item.hasUploadSportSessionValues = hasUpload;
//        NSPredicate *pred = [NSPredicate predicateWithFormat:@"dateTimeStamp == %lld",item.dateTimeStamp];
//        RLMResults *sportItems = [self.sportRecordItems objectsWithPredicate:pred];
//        SportRecordItem *localDateItem = [sportItems firstObject];
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"dateStr == %@",item.dateStr];
        RLMResults *sportDayItems = [self.sportRecordItems objectsWithPredicate:pred];
        SportRecordItem * localDateItem = [sportDayItems firstObject];
        
        if (localDateItem == nil) {
            RLMRealm *realm = [RLMRealm defaultRealm];
            [realm beginWriteTransaction];
            [self.sportRecordItems addObject:item];
            [realm commitWriteTransaction];
        }else{
            if ([localDateItem amountOfCount] < [item amountOfCount]) {
                RLMRealm *realm = [RLMRealm defaultRealm];
                [realm beginWriteTransaction];
                localDateItem.dateTimeStamp = item.dateTimeStamp;
                localDateItem.calories = item.calories;
                localDateItem.dateStr = item.dateStr;
                localDateItem.exerciseTimeInterval = item.exerciseTimeInterval;
                localDateItem.maxSpeed = item.maxSpeed;
                localDateItem.pingJiCount = item.pingJiCount;
                localDateItem.xuanQiuCount = item.xuanQiuCount;
                localDateItem.xueQiuCount = item.xueQiuCount;
                localDateItem.jieJiCount = item.jieJiCount;
                localDateItem.kouShaCount = item.kouShaCount;
                localDateItem.faQiuCount = item.faQiuCount;
                localDateItem.hasReadItem = NO;
                localDateItem.shouldShield = NO;
                localDateItem.hasUploadSportSessionValues = NO;
                [realm commitWriteTransaction];
            }else{
                
            }
        }
    }
}

//保存最新的上传时间戳
- (void)configureLastUploadTimeStamp:(NSString *)lastUploadTimeStamp
{
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm beginWriteTransaction];
    self.lastUploadTime = lastUploadTimeStamp;
    [realm commitWriteTransaction];
}


@end
